﻿using IntoCommon.Util;
using System;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using IntoStationRepair.ViewModels;

namespace IntoStationRepair
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        MainWindowViewModel vm = new MainWindowViewModel();
        private Storyboard OP10Storyboard;
        private Storyboard OP20Storyboard;
        private Storyboard OP30Storyboard;
        private Storyboard OP40Storyboard;
        private bool isOP10Breath = false;
        private bool isOP20Breath = false;
        private bool isOP30Breath = false;
        private bool isOP40Breath = false;
        public MainWindow()
        {
            InitializeComponent();
            this.DataContext = vm;
            OP10Storyboard = (Storyboard)this.FindResource("OP10Breathing");
            OP20Storyboard = (Storyboard)this.FindResource("OP20Breathing");
            OP30Storyboard = (Storyboard)this.FindResource("OP30Breathing");
            OP40Storyboard = (Storyboard)this.FindResource("OP40Breathing");
        }

        private void TextBox_TextChanged(object sender, TextChangedEventArgs e)
        {
            if (GlobalFunc.CheckIsNumberic((sender as TextBox).Text))
            {
                vm.Tighten.SelectProgramNum = Convert.ToInt32((sender as TextBox).Text);
            }
        }

        private void SetProgramDown_Click(object sender, MouseButtonEventArgs e)
        {
            if (vm.Tighten.SelectProgramNum > 0)
            {
                byte[] datas = S7.Net.Types.Int.ToByteArray((short)vm.Tighten.SelectProgramNum);
                vm.Service.WriteData(36, datas);
            }
            else
            {
                MessageBox.Show("输入程序号不合法");
            }
        }

        private void ActionDown_Click(object sender, MouseButtonEventArgs e)
        {
            int bitOffset = Convert.ToInt32((sender as Button).CommandParameter);
            vm.Service.WriteBool(34, bitOffset, true);
        }

        private void ActionUp_Click(object sender, MouseButtonEventArgs e)
        {
            int bitOffset = Convert.ToInt32((sender as Button).CommandParameter);
            vm.Service.WriteBool(34, bitOffset, false);
        }

        private void DataGrid_ScrollChanged(object sender, ScrollChangedEventArgs e)
        {
            if (alarmDg.Items.Count > 0)
                alarmDg.SelectedIndex = 0;
        }

        private void flowInfos_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (flowInfos.SelectedItem != null)
            {
                flowInfos.ScrollIntoView(flowInfos.SelectedItem, null);
            }
        }

        private void Reset_Click(object sender, RoutedEventArgs e)
        {            
            vm.Service.WriteBool(0, 11, true);
            vm.ResetResult = "true";
            Task.Run(() =>
            {
                Thread.Sleep(1000);
                Reset();
                vm.ResetResult = "false";
            });
        }

        private void OP10_Click(object sender, RoutedEventArgs e)
        {            
            vm.OnRepair(0);
            vm.LoadStepTip("OP10", isOP10Breath);
            isOP10Breath = !isOP10Breath;            
            if (isOP10Breath)
            {
                OP10Storyboard.Begin();
            }
            else
            {
                OP10Storyboard.Stop();
                vm.OP10Result = "true";
                EnableAll();
            }
            vm.Service.WriteBool(0, 0, isOP10Breath);
        }

        private void OP20_Click(object sender, RoutedEventArgs e)
        {
            vm.OnRepair(1);
            vm.LoadStepTip("OP20", isOP20Breath);
            isOP20Breath = !isOP20Breath;            
            if (isOP20Breath)
            {
                OP20Storyboard.Begin();
            }
            else
            {
                OP20Storyboard.Stop();
                vm.OP20Result = "true";
                EnableAll();
            }
            vm.Service.WriteBool(0, 2, isOP20Breath);
        }

        private void OP30_Click(object sender, RoutedEventArgs e)
        {
            vm.OnRepair(2);
            vm.LoadStepTip("OP30", isOP30Breath);
            isOP30Breath = !isOP30Breath;
            if (isOP30Breath)
            {
                OP30Storyboard.Begin();
            }
            else
            {
                OP30Storyboard.Stop();
                vm.OP30Result = "true";
                EnableAll();
            }
            vm.Service.WriteBool(0, 4, isOP30Breath);
        }

        private void OP40_Click(object sender, RoutedEventArgs e)
        {
            vm.OnRepair(3);
            vm.LoadStepTip("OP40", isOP40Breath);
            isOP40Breath = !isOP40Breath;
            if (isOP40Breath)
            {
                OP40Storyboard.Begin();
            }
            else
            {
                OP40Storyboard.Stop();
                vm.OP40Result = "true";
                EnableAll();
            }
            vm.Service.WriteBool(0, 6, isOP40Breath);
        }

        private void Confirm_Click(object sender, RoutedEventArgs e)
        {            
            vm.Service.WriteBool(0, 10, true);
            vm.ConfirmResult = "true";            
            Task.Run(() =>
            {
                Thread.Sleep(1000);
                vm.ConfirmResult = "false";
                Reset();
                WriteRfidToMes();
            });
        }

        private void EnableAll()
        {
            vm.IsEnableOP10 = vm.IsEnableOP20 = vm.IsEnableOP30 = vm.IsEnableOP40 = true;
        }

        private void Reset()
        {
            EnableAll();
            isOP10Breath = isOP20Breath = isOP30Breath = isOP40Breath = false;
            vm.OP10Result = vm.OP20Result = vm.OP30Result = vm.OP40Result = "false";
            vm.OP10Code1 = vm.OP10Code2 = vm.OP20Code = vm.OP30Code = vm.OP10Code = string.Empty;
            OP10Storyboard.Stop();
            OP20Storyboard.Stop();
            OP30Storyboard.Stop();
            OP40Storyboard.Stop();
            vm.Service.WriteBool(0, 0, false);
            vm.Service.WriteBool(0, 2, false);
            vm.Service.WriteBool(0, 4, false);
            vm.Service.WriteBool(0, 6, false);
            vm.Service.WriteBool(0, 10, false);
            vm.Service.WriteBool(0, 11, false);
        }

        private void WriteRfidToMes()
        {
            int year = Convert.ToInt32(vm.TrayID.Substring(0, 2));
            int mon = Convert.ToInt32(vm.TrayID.Substring(2, 2));
            int day = Convert.ToInt32(vm.TrayID.Substring(4, 2));
            int hour = Convert.ToInt32(vm.TrayID.Substring(6, 2));
            int min = Convert.ToInt32(vm.TrayID.Substring(8, 2));
            int sec = Convert.ToInt32(vm.TrayID.Substring(10, 2));
            byte[] mesTray = new byte[6] { (byte)year, (byte)mon, (byte)day, (byte)hour, (byte)min, (byte)sec };
            vm.Service.Write40Rfid(170, mesTray);
            SetTrayID();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            SetTrayID();
            Reset();
        }

        private void SetTrayID()
        {
            DateTime time = DateTime.Now;
            var year = time.Year.ToString().Substring(2);
            var mon = time.Month.ToString("D2");
            var day = time.Day.ToString("D2");
            var hour = time.Hour.ToString("D2");
            var min = time.Minute.ToString("D2");
            var sec = time.Second.ToString("D2");
            vm.TrayID = $"{year}{mon}{day}{hour}{min}{sec}";
        }
    }
}
